package com.bdqn.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.pojo.AppInfo;
import com.bdqn.pojo.AppVersion;
import com.bdqn.pojo.DevUser;
import com.bdqn.service.AppInfoService;
import com.bdqn.service.AppVersionService;
import com.bdqn.util.LayData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/ver")
public class AppVersionController {
    @Resource
   private AppVersionService appVersionService;
@Resource
    private AppInfoService appInfoService;
    @RequestMapping(value = "/toversion/{id}")
    public String addsave(@PathVariable("id") Long id,Model model) throws IOException {

        model.addAttribute("xin",id);
        return "jsp/dev/appision";
    }

    @RequestMapping(value = "/tocha/{id}")
    public String tocha(@PathVariable("id") Long id,Model model) throws IOException {
        AppInfo appInfo=appInfoService.getOne(id);
        model.addAttribute("xin",appInfo);
        return "jsp/dev/cha";
    }

    @RequestMapping(value = "/tocha1/{id}")
    public String tocha1(@PathVariable("id") Long id,Model model) throws IOException {
        AppInfo appInfo=appInfoService.getOne(id);
        model.addAttribute("xin",appInfo);
        AppVersion oo=appVersionService.selectIOne(id);
        model.addAttribute("xi",oo);
        return "jsp/backend/cha";
    }

    @RequestMapping(value = "/toupd/{id}")
    public String toupd(@PathVariable("id") Long id,Model model) throws IOException {
       AppVersion pp=appVersionService.selectIOne(id);
        model.addAttribute("xin",pp);
        return "jsp/dev/verupdate";
    }

    @RequestMapping(value = "/upd/{id}")
    public String upd(@PathVariable("id") Long id,HttpSession session,AppVersion appVersion) throws IOException {
        AppVersion pp=appVersionService.selectIOne(id);
        DevUser user=(DevUser)session.getAttribute("user");
        appVersion.setUpdatedBy(user.getId().toString());
        appVersion.setUpdatedTime(new Date());
        appVersion.setId(pp.getId());
        appVersionService.setVer(appVersion);
        return "jsp/dev/list";
    }


    @RequestMapping(value="/list/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getUserList(@PathVariable("id") Long id,@RequestParam("page") Long from, @RequestParam("limit") Long limit){

        LayData data = appVersionService.listAppVersion(from,limit,id);
        return JSONObject.toJSONString(data);
    }


    @RequestMapping(value = "/addVer")
    public String addVer(HttpSession session, AppVersion appVersion) throws IOException {
        DevUser user=(DevUser)session.getAttribute("user");
        appVersion.setPublishstatus(9l);
        appVersion.setCreatedBy(user.getId().toString());
        appVersion.setCreatedTime(new Date());
        appVersionService.addvision(appVersion);
        return "jsp/dev/list";
    }

}
